import { Controller, Get, Header, HttpCode, Post, Query, Redirect, Req, Res } from "@nestjs/common";
import { Request,Response } from "express";
import { CatsService } from "../service/cats.service";
import { Observable, of } from "rxjs";

@Controller("cats")
export class CatsController {

  constructor(private catsService :CatsService) {}

  @Get("promise")
  async findAll(@Req() request: Request): Promise<any[]> {
    return ["This action returns all cat"];
  }
  @Get("ob")
  findAllOB(): Observable<any[]> {
    return of(["hello"]);
  }

  @Post()
  create(): string {
    return "This action create a cat";
  }

  @Get("ab*cd")
  findAllAny() {
    return "This route uses a wildcard";
  }

  //固定状态
  @Get("/http")
  @HttpCode(204)
  httpCode() {
    return "This route uses a wildcard";
  }

//   动态状态
// @Res()
  @Get("/http2")
  httpCode2(@Res() response: Response) {
    // todo status有问题，会报错？
    // response.status(204).send();
  }

  /**
   * 自定义响应头
   */
  @Post("header")
  @Header("Cache-Control", "none")
  createHeader() {
    return "This action adds a new cat";
  }

  /**
   * 重定向
   * @param version
   */
  @Get('docs')
  @Redirect('https://docs.nestjs.com', 302)
  getDocs(@Query('version') version:string) {
    if (version && version === '5') {
      return { url: 'https://docs.nestjs.com/v5/' };
    }
  }
}
